﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.Windows;
using System.Windows.Resources;

namespace CUMTConnect
{
	class Config
	{
		/**
		 * Check if config is available
		 */
		public static bool CheckConfig()
		{
			if (!Config.ConfigFileExist())
			{
				return false;
			}
			else
			{
				if(ConfigurationManager.AppSettings["StudentID"] == "" || ConfigurationManager.AppSettings["Password"] == "" || ConfigurationManager.AppSettings["WLANListenList"] == "")
				{
					return false;
				}
				return true;
			}
		}

		/**
		 * check if config file exists
		 */
		public static bool ConfigFileExist()
		{
			if (System.IO.File.Exists("CUMTConnect.exe.config"))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/**
		 * Create a basic config file
		 */
		public static void ExtracBasicConfig()
		{
			using (StreamWriter sw = new StreamWriter("CUMTConnect.exe.config"))
			{
				sw.Write(Resources.CUMTConnect_exe);
			}
		}

		/**
		 * Load Config
		 */
		public static void LoadConfig(MainWindow mw)
		{
			// Studnet ID
			mw.studentID.Text = ConfigurationManager.AppSettings["StudentID"];

			// Auto login WIFI List
			String[] WIFIList = ConfigurationManager.AppSettings["WLANListenList"].Split(";".ToCharArray());
			mw.WIFIList.Items.Clear();
			for (int i = 0; i < WIFIList.Length; i++)
			{
				if (WIFIList[i] != "")
				{
					mw.WIFIList.Items.Add(WIFIList[i]);
				}
			}

			// Password
			mw.Password.Password = ConfigurationManager.AppSettings["Password"];

			// Carrieroperator
			mw.Carrieroperator.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["Carrieroperator"]);

			// ClickXClose
			mw.ClickXClose.SelectedIndex = Convert.ToInt32(Convert.ToBoolean(ConfigurationManager.AppSettings["ClickXClose"]));

			// CheckIsLogin
			mw.CheckIsLogIn.IsChecked = Convert.ToBoolean(ConfigurationManager.AppSettings["CheckIsLogIn"]);

			// DelaySeconds
			mw.DelaySeconds.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["DelaySecondsIndex"]) - 1;

			// Boot
			//mw.Boot.IsChecked = Convert.ToBoolean(ConfigurationManager.AppSettings["Boot"]);
		}

		/**
		 * Save config
		 */
		public static void SaveConfig(MainWindow mw)
		{
			// check if the basic information was setted
			if (mw.studentID.Text == "" || mw.Password.Password == "" || mw.WIFIList.Items.Count == 0)
			{
				Notice notice = new Notice("请检查你的学号、密码以及被视为校园网接入WIFI列表，它们之中至少一个存在非法配置，程序将无法正常工作，请修改该配置使程序得以正常工作！");
				notice.ShowDialog();
				return;
			}

			Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

			// Studnet ID
			cfa.AppSettings.Settings["StudentID"].Value = mw.studentID.Text;

			// Auto login WIFI List
			StringBuilder stringBuilder = new StringBuilder();
			foreach (var item in mw.WIFIList.Items)
			{
				stringBuilder.Append(item).Append(";");
			}
			cfa.AppSettings.Settings["WLANListenList"].Value = stringBuilder.ToString();

			// Password
			cfa.AppSettings.Settings["Password"].Value = mw.Password.Password;

			// Carrieroperator
			cfa.AppSettings.Settings["Carrieroperator"].Value = Convert.ToString(mw.Carrieroperator.SelectedIndex);

			// ClickXClose
			if (Convert.ToInt32(mw.ClickXClose.SelectedIndex) == 0)
			{
				cfa.AppSettings.Settings["ClickXClose"].Value = "False";
			}
			else
			{
				cfa.AppSettings.Settings["ClickXClose"].Value = "True";
			}

			// CheckIsLogin
			cfa.AppSettings.Settings["CheckIsLogIn"].Value = Convert.ToString(mw.CheckIsLogIn.IsChecked);

			// DelaySeconds
			cfa.AppSettings.Settings["DelaySecondsIndex"].Value = Convert.ToString(mw.DelaySeconds.SelectedIndex + 1);

			// Boot
			//cfa.AppSettings.Settings["Boot"].Value = Convert.ToString(mw.Boot.IsChecked);

			cfa.Save();
			Notice _notice = new Notice("已经保存好了，为使该配置奏效，，本程序将以无界面模式自动重新启动！如果你需要更改配置，可以在托盘图标找到我，双击打开设置界面！");
			_notice.ShowDialog();
			System.Windows.Forms.Application.Restart();
			Application.Current.Shutdown();
		}
	}
}
